package com.atguigu.gulimail.search.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.common.exception.BizCodedEnum;
import com.atguigu.common.to.es.SkuEsModel;
import com.atguigu.common.utils.Response;
import com.atguigu.gulimail.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @author Fengkai
 * @creat 2022/1/5
 */
@RestController
@Slf4j
@RequestMapping("search/save")
public class ElasticSaveController {

    @Autowired
    private ProductSaveService productSaveService;

    @PostMapping("/product")
    //**上架商品
    public Response produceStatusUp(@RequestBody List<SkuEsModel> skuEsModelList){
        log.debug("produceStatusUp接口入参,{}",  JSONObject.toJSONString(skuEsModelList));
        boolean aBoolean = true;
        try {
            aBoolean = productSaveService.produceStatusUp(skuEsModelList);
        } catch (IOException e) {
            log.error("上架调用es接口发生异常，{}", e.getMessage());
            return Response.error(BizCodedEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodedEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }
        if (!aBoolean){
            return Response.ok();
        }else {
            return Response.error(BizCodedEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodedEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }

    }

}
